/*
 * @Description: 事件管理器
 * @version: 1.0.0
 * @Author: CoderXZ
 * @Date: 2020-07-29 23:13:57
 * @LastEditors: CoderXZ
 * @LastEditTime: 2020-08-22 10:26:58
 */


class EventObject {
    eventName: string;//事件名
    eventHandler: Function;//事件响应的回调函数
    isOnce: boolean;//是否只监听一次
    target: Object;//响应的对象
}



export default class EventManager {
    private static m_instance: EventManager = null;

    private m_EventMap: any = {};

    private constructor() {
        this.m_EventMap = {};
    }

    public static getInstance(): EventManager {
        if (!this.m_instance) {
            this.m_instance = new EventManager();
        }

        return this.m_instance;
    }


    // 注册 事件监听
    public on(evtName: string, callback: Function, target: Object, isOnce: boolean = false) {

        // 应该检查是否已经被监听过  evtName callback 和 target都相同

        let evtObj = new EventObject();
        evtObj.eventName = evtName;
        evtObj.target = target;
        evtObj.eventHandler = callback;
        evtObj.isOnce = isOnce;

        if (!this.m_EventMap[evtName]) {
            this.m_EventMap[evtName] = [];
        }

        for (let i = 0; i < this.m_EventMap[evtName].length; i++) {
            let evtObj: EventObject = this.m_EventMap[evtName][i];
            if (evtObj.target === target && evtObj.eventHandler === callback && evtObj.isOnce === isOnce) {
                //已经添加该事件的监听，此处直接结束
                return;
            }
        }

        this.m_EventMap[evtName].push(evtObj);
    }

    //只监听一次
    public once(evtName: string, callback: Function, target: Object) {

        let evtObj = new EventObject();
        evtObj.eventName = evtName;
        evtObj.target = target;
        evtObj.eventHandler = callback;
        evtObj.isOnce = true;

        if (!this.m_EventMap[evtName]) {
            this.m_EventMap[evtName] = [];
        }

        this.m_EventMap[evtName].push(evtObj);
    }


    // 取消事件监听
    public off(evtName: string, target: Object) {
        let evtObjArr: EventObject[] = [];
        if (!this.m_EventMap[evtName]) {
            return;
        }

        evtObjArr = this.m_EventMap[evtName].slice(0);

        for (let i = 0; i < evtObjArr.length; i++) {
            let evtObj: EventObject = evtObjArr[i];
            if (evtObj.target === target) {
                this.m_EventMap[evtName].splice(i, 1);
            }
        }

        if (this.m_EventMap[evtName].length === 0) {
            delete this.m_EventMap[evtName];
        }

        cc.log(this.m_EventMap);
    }


    // 发送事件
    public emit(evtName: string, evtData: Object = null) {
        let evtObjArr: EventObject[] = [];
        if (!this.m_EventMap[evtName]) {
            return;
        }

        evtObjArr = this.m_EventMap[evtName].slice(0);
        for (let i = 0; i < evtObjArr.length; i++) {
            let evtObj: EventObject = evtObjArr[i];
            if (null != evtData && undefined != evtData) {
                evtObj.eventHandler(evtData);
            } else {
                evtObj.eventHandler();
            }

            if (evtObj.isOnce) {
                this.m_EventMap[evtName].splice(i, 1);
            }
        }
    }

    public offAllEvtsByTarget(target: Object) {
        // target为空 或者 this.m_EventMap为空
        if (!target) {
            return;
        }

        for (let evtName in this.m_EventMap) {
            let evtObjects: EventObject[] = this.m_EventMap[evtName];
            for (let i = 0; i < evtObjects.length; i++) {
                let evtObj = evtObjects[i];
                if (evtObj.target === target) {
                    this.m_EventMap[evtName].splice(i, 1);
                }
            }
        }
    }

}

window['GEvent'] = EventManager.getInstance();
